//
//  FeedbackViewController.m
//  Btpin
//
//  Created by Vic on 2020/1/4.
//  Copyright © 2020 Vic. All rights reserved.
//

#import "FeedbackViewController.h"

@interface FeedbackViewController ()<UITextFieldDelegate, UITextViewDelegate>
{
    PSTextView *contentTextView;
    UITextField *phoneTextField;
}
@end

@implementation FeedbackViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (phoneTextField.isFirstResponder) {
        [phoneTextField resignFirstResponder];
    } else if (contentTextView.textView.isFirstResponder) {
        [contentTextView.textView resignFirstResponder];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}
- (void)initUI {
    self.navigationItem.title = @"意见反馈";
    self.view.backgroundColor = THEMECOLOR_OF_WHITE;
    
    UILabel *contentTitleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(ScalePS(20), ScalePS(5), 300, ScalePS(50)) text:@"请详细描述您遇到的问题" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_15];
    [self.view addSubview:contentTitleLbl];
    
    contentTextView = [[PSTextView alloc] initWithFrame:CGRectMake(ScalePS(20), EndY(contentTitleLbl), WIDTH-40*Scale, ScalePS(160)) leftText:@"" text:nil textColor:THEMECOLOR_OF_TEXT_333333 placeholder:@"说说使用产品时遇到的问题" font:Font_15 editable:YES limitMax:LimitText_Max_Number target:self showNumber:YES];
    contentTextView.textView.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:contentTextView];
    contentTextView.backgroundColor = COLOR_OF_WHITE;
    
    UILabel *phoneTitleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(ScalePS(20), EndY(contentTextView)+ScalePS(10), 300, ScalePS(50)) text:@"请留下您的联系方式" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_15];
    [self.view addSubview:phoneTitleLbl];
    
    phoneTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(ScalePS(20), EndY(phoneTitleLbl), WIDTH-ScalePS(40), ScalePS(50)) placeholder:@"手机号、QQ填写一个即可（选填）" secureTextEntry:NO font:Font_15 padding:YES];
    phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    phoneTextField.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:phoneTextField];
    phoneTextField.backgroundColor = COLOR_OF_WHITE;
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(ScalePS(55), EndY(phoneTextField)+ScalePS(80), WIDTH-ScalePS(110), ScalePS(50)) imageName:nil target:self action:@selector(okBtnTap) title:@"提交" titleColor:COLOR_OF_WHITE];
    okBtn.titleLabel.font = Font(Font_18);
    okBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    okBtn.layer.cornerRadius = ScalePS(25);
    okBtn.layer.masksToBounds = YES;
    [self.view addSubview:okBtn];
}
- (void)okBtnTap {
    if (contentTextView.textView.text && contentTextView.textView.text.length) {
        if (contentTextView.textView.text.length>=10) {
            if (phoneTextField.text && phoneTextField.text.length) {
                if ([UIToolsControl validatePhoneNumber:phoneTextField.text] || [UIToolsControl validateQQ:phoneTextField.text]) {
                    [self submitData];
                } else {
                    SVPHUDError(@"您输入的不是手机号或QQ号");
                    [phoneTextField becomeFirstResponder];
                }
            } else {
                [self submitData];
            }
        } else {
            SVPHUDError(@"反馈内容不小于10个字");
            [contentTextView.textView becomeFirstResponder];
        }
    } else {
        SVPHUDError(@"请输入反馈内容");
        [contentTextView.textView becomeFirstResponder];
    }
}
- (void)submitData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setValue:phoneTextField.text forKey:@"contact"];
    [dic setValue:contentTextView.textView.text forKey:@"content"];
    [BaseNetManager requestWithMethod:POST path:Feedback_Suggest_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(@"感谢您的反馈～");
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (phoneTextField.isFirstResponder) {
        [phoneTextField resignFirstResponder];
    } else if (contentTextView.textView.isFirstResponder) {
        [contentTextView.textView resignFirstResponder];
    }
}

@end
